package pers.dean.gobang.boot.socket;
import java.util.Map;

 
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;
 
public class WebSocketInterceptor implements HandshakeInterceptor {
 
    /* 
     * 进入Websocket Handler之前的拦截器
     * 
     */
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse serverHttpResponse, 
    		WebSocketHandler webSocketHandler, Map<String, Object> map) throws Exception {
        if (request instanceof ServletServerHttpRequest) {
        	String id = request.getURI().toString().split("id=")[1];
            map.put("WEBSOCKET_USERID",id);
        }
        return true;
    }
 
    @Override
    public void afterHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, 
    		WebSocketHandler webSocketHandler, Exception e) {
    	System.out.println("进webSocket的afterHandshake拦截器！");
    }
}